<?php

class Sistema_EnviodearquivoController extends App_Controller_Action
{
    
    private $_table     = 'ENVIO_ARQUIVO';
    private $_pk        = 'EVA_ID';
    private $_order_col = 'EVA_DATHOR';
    private $_order     = 'DESC';
    private $_sta_col   = 'EVA_STA';
    private $obj        = '';
    private $form       = '';
    public  $dir        = 'public/upload/enviodearquivo/';
    public  $_file_col  = 'EVA_FILE';
    
    
    private $_PER_ID;
    private $_USU_ID;
    
    public function init()
    {
        /* Initialize action controller here */
        $auth = Zend_Auth::getInstance();
        $s_user = $auth->getStorage();
        $this->_PER_ID = $s_user->read()->PER_ID;
        $this->_USU_ID = $s_user->read()->USU_ID;
    }

    public function indexAction() {
        
         $this->view->PER_ID = $this->_PER_ID;
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('elemform','library');
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_enviodearquivo_list.js');
        
        
        $where = $this->_pk.' > 0';
       
        $itfiltro = $this->_getParam('itfiltro', ''); 
        if($itfiltro != '') $where .= ' AND a.ALU_ID = '. $itfiltro;
        $this->view->itfiltro = $itfiltro; 
        
        $dsfiltro = $this->_getParam('dsfiltro', ''); 
        if($dsfiltro != '') $where .= ' AND EVA_NOM LIKE "%'. $dsfiltro.'%"';
        $this->view->dsfiltro = $dsfiltro;
        
        $query = 'SELECT SQL_CACHE e.*,a.ALU_NOM FROM '.$this->_table.' e, ALUNO a WHERE e.ALU_ID = a.ALU_ID AND '.$where.' ORDER BY '.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
        
    }
    
    public function editAction(){
    
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if(1){
                    $id = (int) $formData[$this->_pk];
                    
                    $returnImg = imagem::upload($this->_file_col,$this->dir,$id);
                    
                    if ($id == 0){
                        $this->db->insert($this->_table,$data);
                        $id = $this->db->lastInsertId();
                        imagem::rename($id,$this->dir,$returnImg['filenew']);
                    }
                 
                    $this->_helper->redirector('index');
                } else {
                    $this->view->data = $formData;
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                $this->view->data = $formData;
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$this->_pk . ' = ' . $id;
                $data = $this->db->fetchRow($query);
                $this->view->data = $data;
            }
        }
        
    }
    

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $data = array($this->_sta_col => 0);
            //$ok = (bool) $this->db->update($this->_table,$data, $this->_pk .' = ' . $id);
            $ok = (bool) $this->db->delete($this->_table,$this->_pk .' = ' . $id);
            
        }
        
        echo json_encode(array('ok' => $ok));
    }
    
}

